import datetime
import uuid

from django.shortcuts import render

# Create your views here.
import logging

from rest_framework.views import APIView

from BookContent import models
from BookContent.utils.searlizers import BaseSearlizer
from shareAPI.utils import JWTencode
from shareAPI.views import APIResponse
from shareAPI2020 import settings


class Book(APIView):
    authentication_classes = []
    def get(self,request,*args,**kwargs):
        pk =kwargs.get('pk')
        print(type(pk))
        # username = request.query_params.get('username')
        # password = request.query_params.get('password')
        pageContentQuerySet = models.BookPageContent.objects.filter(ID=pk).first()
        if not pageContentQuerySet:
            return APIResponse(0,msg='没有拿到页面')
        else:
            print({
                                "Title": pageContentQuerySet.Title,
                                "TypeTitle": pageContentQuerySet.Title,
                                "IntroWord": pageContentQuerySet.IntroWord,
                                "Img": pageContentQuerySet.Img.name,
                                "Content": pageContentQuerySet.Content,  # 图文富文本信息
                                "BookName": pageContentQuerySet.BookName,
                                "author": pageContentQuerySet.author,
                                "master": pageContentQuerySet.master,
                                "GZHName": pageContentQuerySet.BookPageContent_PopInfo.GZHName,
                                "IfShow": pageContentQuerySet.BookPageContent_PopInfo.IfShow,
                                "ImageSRC": pageContentQuerySet.BookPageContent_PopInfo.ImageSRC,

                                })
            return APIResponse({
                                "Title": pageContentQuerySet.Title,
                                "TypeTitle": pageContentQuerySet.Title,
                                "IntroWord": pageContentQuerySet.Title,
                                "Img": pageContentQuerySet.Img.name,
                                "Content": pageContentQuerySet.Content,  # 图文富文本信息
                                "BookName": pageContentQuerySet.BookName,
                                "author": pageContentQuerySet.author,
                                "master": pageContentQuerySet.master,
                                "GZHName": pageContentQuerySet.BookPageContent_PopInfo.GZHName,
                                "IfShow": pageContentQuerySet.BookPageContent_PopInfo.IfShow,
                                "ImageSRC": pageContentQuerySet.BookPageContent_PopInfo.ImageSRC,

                                },msg='success')
        # return APIResponse(0,msg=pk)
        # return JsonResponse(dataJson)

    def post(self,request,*args,**kwargs):
        logging.info(request.data)
        username = request.data.get('username')
        password = request.data.get('password')

        return APIResponse({'code':200,"token":123},msg="登录成功")

class mytest(APIView):
    authentication_classes = []

    def get(self, request, *args, **kwargs):
        print('图片')
        contentQuery = models.BookPageContent.objects.all().first()
        print(contentQuery.mycontent)
        return APIResponse(msg=contentQuery.mycontent)



class ImageSrc(APIView):
    authentication_classes = []
    def get(self,request,*args,**kwargs):
        print('图片')
        srcQuery = models.BookPopInfo.objects.filter(IfShow=1).all().first()
        if not srcQuery:
            return APIResponse({'imgsrc': 'https://www.baidu.com/img/flexible/logo/pc/result.png','ifShow':0}, msg='没找到图片数据')

        print(srcQuery.ImageSRC)
        # username = request.que_params.get('username')
        # password = request.query_params.get('password')

        return APIResponse({'imgsrc':srcQuery.ImageSRC,'ifShow':srcQuery.IfShow},msg='加载图片')

class GridData(APIView):
    authentication_classes = []
    def get(self,request,*args,**kwargs):
        pk = kwargs.get('pk')
        print(pk)
        if not pk and pk!=0:  #path('grid/',views.Book.as_view()),  #返回九宫类型json
            print('返回对应的九宫列表json')
            querySet = models.Type.objects.filter(type_statues=1).all()
            # print(querySet)
            if not querySet:
                return APIResponse(msg='没有对应的数据')
            typeSearlizerQuery = BaseSearlizer.B_Type_serializers_bookContent(instance=querySet,many=True)

            return APIResponse(typeSearlizerQuery.data)
        else:  #path('grid/<int:pk>',views.Book.as_view()),  返回某一个类型下面的所有标题和id
            print('返回某个类型下所有的标题和pageid')
            print(pk)

            querySet = models.BookPageContent.objects.filter(ifShow=1,score_type__id=pk).all()
            if not querySet:
                return APIResponse(msg='没有对应的数据')
            typeSearlizerQuery = BaseSearlizer.B_XXXTypeList_serializers_bookContent(instance=querySet,many=True)

            return APIResponse(typeSearlizerQuery.data)

class indexList(APIView):
    authentication_classes = []
    def get(self,request,*args,**kwargs):
        print('返回某个类型下所有的标题和pageid')
        querySet = models.BookPageContent.objects.filter(ifShowIndex=1).all()
        if not querySet:
            return APIResponse(msg='没有对应的数据')
        typeSearlizerQuery = BaseSearlizer.B_XXXTypeList_serializers_bookContent(instance=querySet,many=True)

        return APIResponse(typeSearlizerQuery.data)




